/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ibmmodel3;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author mwh
 */
public class DistortionTableGrep implements DistortionTable {

    String filename;


    public DistortionTableGrep(String filename) {
        this.filename = filename;
    }


    public double get(DistortionKey k) {
        double res = Double.NaN;

        try {
            /**
             * Giza does not use the real targetLength so don't use this in the search
             */
            String line = Grep.grepFirst("^" + k.SourcePos + " " + k.TargetPos + " " + k.SourceLength + " " + "\\d+", filename);
            String[] tokens = line.split(" ");
            res = Double.parseDouble(tokens[4]);

        } catch (FileNotFoundException ex) {
            Logger.getLogger(VocabularyTableGrep.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(VocabularyTableGrep.class.getName()).log(Level.SEVERE, null, ex);
        }
        return res;
    }


    public double getLog10(DistortionKey k) {
        return Math.log10(get(k));
    }
}
